package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemRiskChildBinding;
import com.example.yichuangyun.databinding.ItemRiskInfoBinding;
import com.example.yichuangyun.model.company.RiskInfoDataModel;
import com.example.yichuangyun.utils.CommonUtil;

import java.util.List;

/**
 * 工商变更
 */
public class RiskInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<RiskInfoDataModel> data;

    public RiskInfoAdapter(Context context, List<RiskInfoDataModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemRiskInfoBinding binding = ItemRiskInfoBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            RiskInfoDataModel model = data.get(position);
            switch (position) {
                case 0:
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getTabnormalList().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getTabnormalList(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getTabnormalList().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getTabnormalList().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getTabnormalList().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 1:
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getTadministrativePunishments().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getTadministrativePunishments(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getTadministrativePunishments().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getTadministrativePunishments().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getTadministrativePunishments().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 2:
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getTseriousIllegals().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getTseriousIllegals(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getTseriousIllegals().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getTseriousIllegals().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getTseriousIllegals().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 3:
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getTotalData().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getTotalData(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getTotalData().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getTotalData().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getTotalData().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
            }
            myViewHolder.binding.recyclerView.setVisibility(model.isOpen() ? View.VISIBLE : View.GONE);
            myViewHolder.binding.ivArrow.setImageResource(model.isOpen() ? R.mipmap.icon_up : R.mipmap.icon_down);
            myViewHolder.binding.tvTitle.setText(model.getTitle());

            myViewHolder.binding.llTitle.setEnabled(data.size() > 0);
            myViewHolder.binding.llTitle.setOnClickListener(view -> {
                model.setOpen(!model.isOpen());
                notifyItemChanged(holder.getAbsoluteAdapterPosition());
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemRiskInfoBinding binding;

        public MyViewHolder(ItemRiskInfoBinding itemRiskInfoBinding) {
            super(itemRiskInfoBinding.getRoot());
            binding = itemRiskInfoBinding;
        }
    }

    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final List<RiskInfoDataModel.RiskModel> mData;
        private final int parentPosition;

        public MyAdapter(List<RiskInfoDataModel.RiskModel> mData, int parentPosition) {
            this.mData = mData;
            this.parentPosition = parentPosition;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ItemRiskChildBinding binding = ItemRiskChildBinding.inflate(LayoutInflater.from(context), parent, false);
            return new RiskViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof RiskViewHolder) {
                RiskViewHolder myViewHolder = (RiskViewHolder) holder;
                RiskInfoDataModel.RiskModel model = mData.get(position);
                switch (parentPosition) {
                    case 0: // 经营异常
                        myViewHolder.binding.tvText5.setVisibility(View.VISIBLE);
                        myViewHolder.binding.tvText6.setVisibility(View.GONE);
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.in_date, CommonUtil.setText3(model.getInDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.decide_org, CommonUtil.setText3(model.getDepartment())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.abnormal_cause, CommonUtil.setText3(model.getInReason())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.remove_date, CommonUtil.setText3(model.getOutDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.remove_cause, CommonUtil.setText3(model.getOutReason())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                    case 1: // 行政处罚
                        myViewHolder.binding.tvText5.setVisibility(View.VISIBLE);
                        myViewHolder.binding.tvText6.setVisibility(View.GONE);
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.punish_date, CommonUtil.setText3(model.getDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.decide_num, CommonUtil.setText3(model.getNumber())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.punish_cause, CommonUtil.setText3(model.getReason())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.punish_unit, CommonUtil.setText3(model.getDepartment())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.punish_result, CommonUtil.setText3(model.getContent())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                    case 2: // 严重违法失信
                        myViewHolder.binding.tvText5.setVisibility(View.VISIBLE);
                        myViewHolder.binding.tvText6.setVisibility(View.VISIBLE);
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.in_date, CommonUtil.setText3(model.getInDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.int_cause, CommonUtil.setText3(model.getInReason())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.int_org, CommonUtil.setText3(model.getInDepartment())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.remove_cause, CommonUtil.setText3(model.getOutReason())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.remove_date, CommonUtil.setText3(model.getOutDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText6.setText(Html.fromHtml(context.getString(R.string.remove_org, CommonUtil.setText3(model.getOutDepartment())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                    case 3: // 税务异常（非正常户表+重大税收违法）
                        if (model.getType() == 1) { // 异常类型 0重大税收违法 1非正常户
                            myViewHolder.binding.tvText5.setVisibility(View.VISIBLE);
                            myViewHolder.binding.tvText6.setVisibility(View.GONE);
                            myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.abnormal_type_fzch), Html.FROM_HTML_MODE_LEGACY));
                            myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.sure_date, CommonUtil.setText3(model.getJudgeDate())), Html.FROM_HTML_MODE_LEGACY));
                            myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.tax_num, CommonUtil.setText3(model.getTaxNum())), Html.FROM_HTML_MODE_LEGACY));
                            myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.tax_type, CommonUtil.setText3(model.getOverdueType())), Html.FROM_HTML_MODE_LEGACY));
                            myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.tax_money, CommonUtil.setText3(model.getOverdueAmount())), Html.FROM_HTML_MODE_LEGACY));
                        } else {
                            myViewHolder.binding.tvText5.setVisibility(View.GONE);
                            myViewHolder.binding.tvText6.setVisibility(View.GONE);
                            myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.abnormal_type_zdsswf), Html.FROM_HTML_MODE_LEGACY));
                            myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.publish_date, CommonUtil.setText3(model.getTime())), Html.FROM_HTML_MODE_LEGACY));
                            myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.tax_org, CommonUtil.setText3(model.getPubDepartment())), Html.FROM_HTML_MODE_LEGACY));
                            myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.case_type, CommonUtil.setText3(model.getCaseType())), Html.FROM_HTML_MODE_LEGACY));
                        }
                        break;
                }
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }

    static class RiskViewHolder extends RecyclerView.ViewHolder {

        ItemRiskChildBinding binding;

        public RiskViewHolder(ItemRiskChildBinding itemRiskChildBinding) {
            super(itemRiskChildBinding.getRoot());
            binding = itemRiskChildBinding;
        }
    }
}
